package com.jeecms.wicool.manager.groupBuy.impl;

import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Shorts;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.groupBuy.GroupBuyDao;
import com.jeecms.wicool.entity.groupBuy.GroupBuy;
import com.jeecms.wicool.entity.groupBuy.GroupBuySpecification;
import com.jeecms.wicool.entity.orders.OrderProductMapping;
import com.jeecms.wicool.entity.orders.Orders;
import com.jeecms.wicool.entity.user.UserAccount;
import com.jeecms.wicool.manager.groupBuy.GroupBuyMng;
import com.jeecms.wicool.manager.orders.OrderProductMappingMng;
import com.jeecms.wicool.manager.orders.OrdersMng;
import com.jeecms.wicool.manager.user.UserAccountMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * Created by cuiyanlei on 15/6/10.
 */
@Transactional
@Service
public class GroupBuyMngImpl implements GroupBuyMng {
    @Override
    public GroupBuy save(GroupBuy bean) {
        return dao.save(bean);
    }

    @Override
    public GroupBuy deleteById(Integer id) {
        return dao.deleteById(id);
    }

    @Override
    public GroupBuy updateByUpdater(Updater<GroupBuy> updater) {
        return dao.updateByUpdater(updater);
    }

    @Override
    public GroupBuy findById(Integer id) {
        return dao.findById(id);
    }

    @Override
    public Pagination getPage(Integer productId, String content, int pageNo, int pageSize) {
        return dao.getPage(productId, content, pageNo, pageSize);
    }

    @Override
    @Transactional
    public void confirm(Integer id) {
        GroupBuy groupBuy = dao.findById(id);
        List<Orders> groupBuyOrders = ordersMng.getAllGroupBuyOrders(id);//获取所有拼单的订单
        Integer saleCount = groupBuy.getSaleCount();
        Set<GroupBuySpecification> specifications = groupBuy.getSpecifications();
        Ordering<GroupBuySpecification> ordering = new Ordering<GroupBuySpecification>() {
            @Override
            public int compare(GroupBuySpecification groupBuySpecification, GroupBuySpecification t1) {
                return Ints.compare(groupBuySpecification.getBoundaryCount(), t1.getBoundaryCount());
            }
        };
        List<GroupBuySpecification> sortedSpecifications = ordering.reverse().sortedCopy(specifications);
        GroupBuySpecification specification = null;
        //获取价格档
        for (GroupBuySpecification sortedSpecification : sortedSpecifications) {
            if (saleCount >= sortedSpecification.getBoundaryCount()) {
                specification = sortedSpecification;
                break;
            }
        }
        for (Orders groupBuyOrder : groupBuyOrders) {
            groupBuyOrder.setStatus(Shorts.checkedCast(2));
            List<OrderProductMapping> orderProductMappings = orderProductMappingMng.getByOrderId(groupBuyOrder.getId());
            if (orderProductMappings.size() > 0) {
                OrderProductMapping orderProductMapping = orderProductMappings.get(0);
                Integer buyCount = orderProductMapping.getBuyCount();
                if (null != specification) {
                    //计算差价，总价-数量*折后价
                    BigDecimal backPrice = groupBuyOrder.getOrderPrice().subtract(specification.getPrice().multiply(new BigDecimal(buyCount)));
                    groupBuyOrder.setBackPrice(backPrice);
                    UserAccount userAccount = userAccountMng.getListUserAccountByUserId(groupBuyOrder.getUser().getId()).get(0);
                    userAccount.setBalance(userAccount.getBalance().add(backPrice));
                    userAccountMng.update(userAccount, userAccount.getShop().getId(), groupBuyOrder.getUser().getId());//返还余额
                }
            }
        }
        groupBuy.setStatus(2);
        dao.updateByUpdater(new Updater<GroupBuy>(groupBuy));
    }

    @Autowired
    private GroupBuyDao dao;

    @Autowired
    private OrdersMng ordersMng;

    @Autowired
    private OrderProductMappingMng orderProductMappingMng;

    @Autowired
    private UserAccountMng userAccountMng;
}
